package com.atguigu.dao.impl;

import com.atguigu.dao.TomcatServiceConfigDao;
import com.atguigu.pojo.TomcatServiceConfig;

import java.util.List;

public class TomcatServiceConfigDaoImpl extends BaseDao implements TomcatServiceConfigDao {
    @Override
    public int addTomcatServiceConfig(TomcatServiceConfig tomcatServiceConfig) {
        System.out.println("TomcatServiceConfigDaoImpl 程序在["+Thread.currentThread().getName()+"]线程中");
        String sql="insert into t_tomcatServiceConfig(`id`,`serverId`,`serviceId`,`tomcatHome`,`javaHome`,`maxMemory`,`httpPort`,`shutdownPort`,`projectId`,`moduleId`,`use`,`heartbeat`) values(?,?,?,?,?,?,?,?,?,?,?,?);";
            return update(sql,tomcatServiceConfig.getId(),tomcatServiceConfig.getServerId(),
                    tomcatServiceConfig.getServiceId(),tomcatServiceConfig.getTomcatHome(),tomcatServiceConfig.getJavaHome(),tomcatServiceConfig.getMaxMemory(),
                    tomcatServiceConfig.getHttpPort(),tomcatServiceConfig.getShutdownPort(),tomcatServiceConfig.getProjectId(),
                    tomcatServiceConfig.getModuleId(),tomcatServiceConfig.getUse(),tomcatServiceConfig.getHeartbeat());
    }

    @Override
    public int deleteTomcatServiceConfigById(String id) {
        String sql="delete from t_tomcatServiceConfig where id=?";
        return update(sql,id);
    }

    @Override
    public int updateTomcatServiceConfig(TomcatServiceConfig tomcatServiceConfig) {
        String sql="update t_tomcatServiceConfig set `serverId`=?,`serviceId`=?,`tomcatHome`=?,`javaHome`=?,`maxMemory`=?,`httpPort`=?,`shutdownPort`=?,`projectId`=?,`moduleId`=?,`use`=?,`heartbeat`=? where id=?";
        return update(sql,tomcatServiceConfig.getServerId(),
                tomcatServiceConfig.getServiceId(),tomcatServiceConfig.getTomcatHome(),
                tomcatServiceConfig.getJavaHome(),tomcatServiceConfig.getMaxMemory(),tomcatServiceConfig.getHttpPort(),tomcatServiceConfig.getShutdownPort(),tomcatServiceConfig.getProjectId(),tomcatServiceConfig.getModuleId(),tomcatServiceConfig.getUse(),tomcatServiceConfig.getHeartbeat(),tomcatServiceConfig.getId());
    }

    @Override
    public TomcatServiceConfig queryTomcatServiceConfigById(String id) {
        String sql ="select `id`,`serverId`,`serviceId`,`tomcatHome`,`javaHome`,`maxMemory`,`httpPort`,`shutdownPort`,`projectId`,`moduleId`,`use`,`heartbeat` from t_tomcatServiceConfig where id=?";
        return queryForOne(TomcatServiceConfig.class,sql,id);
    }

    @Override
    public List<TomcatServiceConfig> queryTomcatServiceConfigs() {
        String sql ="select `id`,`serverId`,`serviceId`,`tomcatHome`,`javaHome`,`maxMemory`,`httpPort`,`shutdownPort`,`projectId`,`moduleId`,`use`,`heartbeat`  from t_tomcatServiceConfig";
        return queryForList(TomcatServiceConfig.class,sql);
    }
    @Override
    public TomcatServiceConfig queryTomcatServiceConfigByServiceId(String serviceId){
        String sql ="select `id`,`serverId`,`serviceId`,`tomcatHome`,`javaHome`,`maxMemory`,`httpPort`,`shutdownPort`,`projectId`,`moduleId`,`use`,`heartbeat` from t_tomcatServiceConfig where serviceId =?";
        return queryForOne(TomcatServiceConfig.class,sql,serviceId);
    }
    @Override
    public List<TomcatServiceConfig> queryTomcatServiceConfigsByProjectModuleId(String projectModuleId){
        String sql ="select `id`,`serverId`,`serviceId`,`tomcatHome`,`javaHome`,`maxMemory`,`httpPort`,`shutdownPort`,`projectId`,`moduleId`,`use`,`heartbeat` from t_tomcatServiceConfig where moduleId =?";
        return queryForList(TomcatServiceConfig.class,sql,projectModuleId);
    }

    @Override
    public Integer queryForPageTotalCount() {
        String sql="select count(*) from t_tomcatServiceConfig";
        Number serverCount=(Number) queryForSingleValue(sql);//这个语句似乎可以返回任何类型的查询
        //System.out.println(bookCount);
        return serverCount.intValue();
    }

    @Override
    public List<TomcatServiceConfig> queryForPageItems(Integer begin, Integer pageSize) {
        String sql ="select `id`,`serverId`,`serviceId`,`tomcatHome`,`javaHome`,`maxMemory`,`httpPort`,`shutdownPort`,`projectId`,`moduleId`,`use`,`heartbeat` from t_tomcatServiceConfig limit ?,?";
        return queryForList(TomcatServiceConfig.class,sql,begin,pageSize);

    }
}
